package com.rayenterprise.kms.widgets;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HasAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.rayenterprise.kms.client.Kms4gwt;
import com.rayenterprise.kms.client.remote.NotionRemote;
import com.rayenterprise.kms.domain.KNotion;
import com.rayenterprise.kms.events.AfterNotionNodeChangeEvent;

public class NotionEditTab extends AbstractCustomEditTab {

	KNotion notion = new KNotion();
	NotionEditTab self = this;
	public NotionEditTab(Object obj, AbstractCustomNode node,
			HandlerManager eventBus) {
		super(obj, node, eventBus);
		this.notion = (KNotion) obj;
		text.setText(notion.getName());
	}

	@Override
	public void customObjectInit() {

		name.setText("Notion Name:");
		tab.setAnimationEnabled(true);
		tab.add(hp, "save notion");
		tab.selectTab(0);

		button.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				notion.setName(text.getValue());
				NotionRemote.Util.getInstance().save(notion,
						new AsyncCallback<KNotion>() {

							@Override
							public void onFailure(Throwable caught) {
								// TODO Auto-generated method stub
								name.setText(caught.getMessage());

							}

							@Override
							public void onSuccess(KNotion result) {
								node.setLblText(result.getName());
							}
						});

			}
		});

		/*
		 * Add Info Tab and its behaviors
		 */
		if (node != null) {

			HorizontalPanel hpNotion = new HorizontalPanel();
			Button btn = new Button("new info");
			//btn.setStyleName("form-button");
			
			hpNotion.setVerticalAlignment(HasAlignment.ALIGN_MIDDLE);
		
			hpNotion.add(btn);
			hpNotion.setSpacing(5);

			tab.add(hpNotion, "new info");
			btn.addClickHandler(new ClickHandler() {

				@Override
				public void onClick(ClickEvent event) {
					Kms4gwt.get().setWidget(new InfoEditor(node,eventBus));
					self.hide();
				}
			});
			// Add Delete Tab
			Label lblDelete = new Label("Delete This Notion? ");
			lblDelete.setStyleName("form-label");
			Button btnDelete = new Button("delete");
			//btnDelete.setStyleName("form-button");
			HorizontalPanel delHp = new HorizontalPanel();
			delHp.setSpacing(5);
			delHp.setVerticalAlignment(HasAlignment.ALIGN_MIDDLE);
			delHp.add(lblDelete);
			delHp.add(btnDelete);

			tab.add(delHp, "delete notion");
			btnDelete.addClickHandler(new ClickHandler() {

				@Override
				public void onClick(ClickEvent event) {
					if (Window.confirm("Realy want to delete this Notion?")) {
						NotionRemote.Util.getInstance().deleteNotion(notion,
								new AsyncCallback<Void>() {

									@Override
									public void onFailure(Throwable caught) {
										// TODO Auto-generated method stub

									}

									@Override
									public void onSuccess(Void result) {
										// TODO Auto-generated method stub
										Window.alert("Delete Successful!");
										AfterNotionNodeChangeEvent sent = new AfterNotionNodeChangeEvent(
												node);
										eventBus.fireEvent(sent);

									}
								});
					}

				}

			});
		}

		this.add(tab);
		this.setWidth("350px");

	}

}
